<?php
include("model/suppliers.php");
class suppliersController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_suppliers_box = $this->getListSupplierBox();
		$smarty->assign("list_suppliers_box",$list_suppliers_box);
		
		$content = $smarty->fetch("suppliers/suppliers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListSupplierBox(){
		$smarty 		= $_SESSION['smarty'];
		$supplier_obj 	= new suppliers();
		$supplierList	= $supplier_obj->getSupplierList();		
		$smarty->assign("supplierList", $supplierList);
		$html 			= $smarty->fetch("suppliers/suppliers_list.html");
		return $html;
	}
	
	function insertAction(){
		$suppliers_obj = new suppliers();
		$data 		= array();
		$data['sup_name'] = $_REQUEST['sup_name'];
		$data['sup_address'] = $_REQUEST['sup_address'];
		$data['sup_phone'] = $_REQUEST['sup_phone'];
		
		$result = $suppliers_obj->insertASupplier($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("suppliers/suppliers_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$sup_id = $_REQUEST['args'];
		$suppliers_obj = new suppliers();
		$result = $suppliers_obj->inactivateASupplier($sup_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_suppliers_box = $this->getListSupplierBox();
		$smarty->assign("list_suppliers_box",$list_suppliers_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("suppliers/suppliers.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$sup_id = $_REQUEST['args'];
		$suppliers_obj = new suppliers();
		$result = $suppliers_obj->activateASupplier($sup_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_suppliers_box = $this->getListSupplierBox();
		$smarty->assign("list_suppliers_box",$list_suppliers_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("suppliers/suppliers.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$insertform_suppliers_box = $smarty->fetch("suppliers/suppliers_insert_form.html");
		$smarty->assign("insertform_suppliers_box",$insertform_suppliers_box);
		
		$content = $smarty->fetch("suppliers/suppliers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$sup_id 			= $_REQUEST['args'];
		$smarty 		= $_SESSION['smarty'];		
		
		$suppliers_obj 		= new suppliers();
		$current_data 		= array();
		$current_data 		= $suppliers_obj->getSuppliersInfo($sup_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_suppliers_box = $smarty->fetch("suppliers/suppliers_edit_form.html");
		$smarty->assign("editform_suppliers_box",$editform_suppliers_box);
		
		$content = $smarty->fetch("suppliers/suppliers.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");	
	}
	
	function editAction(){
		$suppliers_obj = new suppliers();
		$data 		= array();
		$data['sup_id'] 		= $_REQUEST['sup_id'];
		$data['sup_name'] 	= $_REQUEST['sup_name'];
		$data['sup_address'] 	= $_REQUEST['sup_address'];
		$data['sup_phone'] 	= $_REQUEST['sup_phone'];
		
		
		$result = $suppliers_obj->editASupplier($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("suppliers/suppliers_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
}
?>